import Mock from 'mockjs';
const users = [
    { username: 'admin', password: '123456' ,role:'admin'},
    { username: 'text', password: '123456',role:'user' }
];
// 模拟登录接口
Mock.mock('/api/login', 'post', (options) => {
    const { username, password } = JSON.parse(options.body);
    const user = users.find(u => u.username === username && u.password === password);
    if (user) {
        const token = Mock.Random.guid()
        return {
            code: 200,
            message: '登录成功',
            data: {
                user,
                token
            }
        };
    } else {
        return {
            code: 401,
            message: '用户名或密码错误'
        };
    }
});

// 模拟验证 Token 接口
Mock.mock('/api/validateToken', 'post', (options) => {
    const { token } = JSON.parse(options.body);
    // 简单模拟验证，实际应根据具体逻辑
    if (token) {
        return {
            code: 200,
            message: 'Token 验证成功',
            data: {
                user: { username: 'admin', password: '123456' ,role:'admin'}
            }
        };
    } else {
        return {
            code: 401,
            message: 'Token 验证失败'
        };
    }
});
// 模拟注册接口
Mock.mock('/api/register', 'post', (options) => {
    const { username, password } = JSON.parse(options.body);
    const userExists = users.some(u => u.username === username);
    if (userExists) {
        return {
            code: 400,
            message: '用户名已存在'
        };
    } else {
        return {
            code: 200,
            message: '注册成功'
        };
    }
});
// 模拟检查已上传分片接口
Mock.mock('/api/checkChunks', 'post', (options) => {
    const { filename, totalChunks } = JSON.parse(options.body);
    // 简单模拟，这里可以根据实际情况从存储中获取已上传的分片信息
    const uploadedChunks = [];
    return {
        code: 200,
        message: '检查成功',
        data: {
            uploadedChunks
        }
    };
});

// 模拟上传分片接口
Mock.mock('/api/uploadChunk', 'post', (options) => {
    console.log('options',options)
    // 模拟上传成功
    return {
        code: 200,
        message: '分片上传成功'
    };
});

// 模拟合并分片接口
Mock.mock('/api/mergeChunks', 'post', (options) => {
    const { filename, totalChunks, uploadUser, uploadTime } = JSON.parse(options.body);
    // 模拟文件存储到指定目录
    // 这里可以添加实际的文件合并和存储逻辑
    console.log(`文件 ${filename} 合并完成，上传用户：${uploadUser}，上传时间：${uploadTime}`);
    return {
        code: 200,
        message: '合并分片成功'
    };
});